const Users = require('../models/Users');
const { hash, compare } = require('../utils/hash');

//查询所有用户
const getUsersAll = async () => {
  let result = await Users.findAll();
  return result;
};

//根据用户名查找用户
const findUserByUsername = async (username, type) => {
  let result = await Users.findOne({
    where: {
      username,
      type,
    },
  });
  // console.log(result);
  if (result) {
    return {
      msg: '查找成功',
      data: result,
      status: 1,
    };
  } else {
    return {
      msg: '查找失败',
      status: 0,
    };
  }
};

// 添加用户
const addUser = async ({ username, password, type }) => {
  let result = await findUserByUsername(username, type);
  if (result.msg === '查找成功') {
    return {
      msg: '该用户已经注册了 请更换用户名',
      status: 0,
    };
  } else {
    let result = await Users.create({
      username,
      password,
      type,
    });
    return {
      msg: '注册成功',
      status: 1,
      data: result.toJSON(),
    };
  }
};

//删除用户
const deleteUser = async ({ username, type }) => {
  let result = await Users.destroy({
    where: {
      username,
      type,
    },
  });
  if (result) {
    return {
      msg: '删除成功',
      status: 1,
    };
  } else {
    return {
      msg: '删除失败',
      status: 0,
    };
  }
};

//修改密码
const upPassword = async ({ username, type, password, oldPassword }) => {
  //将password和数据库中的密码进行比较
  let result = await findUserByUsername(username, type);
  if (result.msg === '查找失败') {
    return {
      msg: '用户不存在',
      status: 0,
    };
  } else {
    let user = result.data;
    let { password: dbPassword } = user;
    console.log(dbPassword, password, oldPassword);
    if (compare(oldPassword, dbPassword)) {
      user.password = password;
      await user.save();
      return {
        msg: '密码修改成功',
        status: 1,
      };
    } else {
      return {
        msg: '原密码错误',
        status: 0,
      };
    }
  }
};

//登录
const loginUser = async ({ username, password, type }) => {
  //将password和数据库中的密码进行比较
  let result = await findUserByUsername(username, type);
  if (result.msg === '查找失败') {
    return {
      msg: '用户不存在',
      status: 0,
    };
  } else {
    let user = result.data;
    let { username: dbUsername, password: dbPassword } = user;
    if (dbUsername === username && compare(password, dbPassword)) {
      return {
        msg: '登录成功',
        status: 1,
        user,
      };
    } else {
      return {
        msg: '密码错误',
        status: 0,
      };
    }
  }
};

//修改拓展信息
const upUserInfo = async (data) => {
  let result = await findUserByUsername(data.username, data.type);
  if (result.msg === '查找失败') {
    return {
      msg: '用户不存在',
      status: 0,
    };
  } else {
    let user = result.data;
    user.tel = data.tel;
    user.email = data.email;
    user.motto = data.motto;
    user.phone = data.phone;
    user.job = data.job;
    user.birthday = data.birthday;
    user.signature = data.signature;
    await user.save();
    return {
      msg: '修改成功',
      status: 1,
    };
  }
};

//修改头像
const upAvatar = async (data, url) => {
  let result = await findUserByUsername(data.username, data.type);
  if (result.status == 0) {
    return {
      msg: '用户不存在',
      status: 0,
    };
  } else {
    let user = result.data;
    user.url = url;
    await user.save();
    return {
      msg: '修改成功',
      status: 1,
    };
  }
};

//重置密码
const resetPassword = async ({ username, type }) => {
  let result = await findUserByUsername(username, type);
  if (result.status == 0) {
    return {
      msg: '用户不存在',
      status: 0,
    };
  } else {
    let user = result.data;
    user.password = '111111';
    await user.save();
    return {
      msg: '重置成功',
      status: 1,
    };
  }
};
module.exports = {
  addUser,
  findUserByUsername,
  getUsersAll,
  deleteUser,
  upPassword,
  loginUser,
  upUserInfo,
  upAvatar,
  resetPassword,
};
